package model;

/**
 *
 * @author Dustin
 */
public class Queue {
    private Object head;
    private Queue tail;

    public int size() {
        int size = 0;
        if(!isEmpty()) {
            size++;
            size += tail.size();
        }
        return size;
    }

    public boolean isEmpty() {
        return head == null && tail == null;
    }

    public Object getHead() {
        return head;
    }

    public void enqueue(Object value) {
        try {
            if(isEmpty()) {
                head = value;
                tail = new Queue();
            } else {
                tail.enqueue(value);
            }
        } catch(NullPointerException ex) {
            System.out.println(value);
            ex.printStackTrace();
        }
    }

    public Object dequeue() {
        Object tempHead = head;
        try{
            if(!isEmpty()) {
                if(!tail.isEmpty()) {
                    head = tail.head;
                    tail.dequeue();
                } else {
                    head = null;
                    tail = null;
                }
            } else {
                head = null;
                tail = null;
            }
        } catch(NullPointerException ex) {
            System.out.println(tempHead);
            ex.printStackTrace();
        }
        return tempHead;
    }
}
